home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / MacMud / Mud 4.0 / lpconsole.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-25  |  20.7 KB  |  929 lines  |  [TEXT/MPS ]

  1.  
  2. #include <XtcLib.h>
  3. #include <XtcElements.h>
  4. #include <XtcMessages.h>
  5.  
  6. #include "config.h"
  7. #include "rc.h"
  8. #include "MacMud.h"
  9. #include "mac.h"
  10.  
  11. #include ":console:ed.h"
  12. #include ":console:buffer.h"
  13. #include ":console:command.h"
  14.  
  15. #include <sys/types.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include "comm.h"
  19.  
  20. #define KBUFFSIZE    4096
  21. #define kLEFTSPACE    4
  22.  
  23. extern int cb_append(cmdbuf *cb);
  24.  
  25. typedef struct TConsole {
  26.     short        penx;
  27.     short        currx;
  28.     UI_ViewPtr    view;
  29.     Boolean        caretHidden;
  30.     Boolean        caretVisible;
  31.     Boolean        consoleActive;
  32.     ulong        caretTime;
  33.     uchar        inbuffer[KBUFFSIZE + 1];
  34.     uchar        outbuffer[KBUFFSIZE + 1];
  35.     short        incount;
  36.     short        outcount;
  37.     int            topoffset;
  38.     Rect        bounds;
  39.     cmdbuf        *cb;
  40.     char        *tmpfname;
  41. } TConsole, *TConsolePtr, **TConsoleHdl;
  42.  
  43. extern Boolean gRunning;        // Defined in MacMud.c
  44. extern Boolean gBackgroundOnly;    // Defined in MacMud.c
  45. extern long gMudResponseFactor;    // Defined in <mac.h>
  46. extern short gMudLibVolRef;        // Defined in <mac.h>
  47. extern long gMudLibDirID;        // Defined in <mac.h>
  48.  
  49. pascal Boolean XtcDebug(UI_ViewPtr view, UI_Action *action);
  50. pascal Boolean XtcConsole(UI_ViewPtr view, UI_Action *action);
  51. pascal Boolean XtcMain(UI_ViewPtr view, UI_Action *action);
  52.  
  53. /********************************************************************
  54. *            #defines... should probably be in header file...        *
  55. *********************************************************************/
  56. #define TRUE 1
  57. #define FALSE 0
  58. #define HiWrd(aLong)    (((aLong) >> 16) & 0xFFFF)
  59. #define LoWrd(aLong)    ((aLong) & 0xFFFF)
  60.  
  61. static int debugLog;
  62. static int tempCount = 0;
  63. static RgnHandle updateRgn = NULL;
  64. static UI_ViewPtr debugView = NULL;
  65. extern int __write(int fildes, char* buf, unsigned long nbyte);
  66.  
  67. pascal Boolean SFEmptyFileFilter(ParmBlkPtr paramBlock)
  68. {
  69. #pragma unused(paramBlock)
  70.     return true;
  71. }
  72.  
  73. static Boolean gDirSemaphore = false;
  74.  
  75. pascal short SFDirectoryDlgHook(short item, DialogPtr theDialog)
  76. {
  77. #pragma unused(theDialog)
  78.  
  79.     const short kCancel = 3;
  80.     const short kDirButton = 11;
  81.  
  82.     if (item == kDirButton) {
  83.         gDirSemaphore = true;
  84.         return kCancel;
  85.     } else return item;
  86. }
  87.  
  88. Boolean ChangeMudlibDir(long *dirID, short *volRef)
  89. {
  90.     Point thePt;
  91.     SFReply theReply;
  92.     SFTypeList typeList;
  93.     const short kDirDlg = 4002;
  94.  
  95.     SetPt(&thePt, 0, 0);
  96.     gDirSemaphore = false;
  97.     SFPGetFile(thePt, "\p", SFEmptyFileFilter, 0, &typeList,
  98.                     SFDirectoryDlgHook, &theReply, kDirDlg, NULL);
  99.     if (gDirSemaphore) {
  100.         *dirID = (*(long *)0x398);
  101.         *volRef = -(*(short *)0x0214);
  102.         return true;
  103.     }
  104.     return false;
  105. }
  106.  
  107. void InitConsole()
  108. {
  109.     debugView = NULL;
  110.     if (!gBackgroundOnly) {
  111.         debugView = UI_LoadView(kDebugView);
  112.         if (debugView == NULL)
  113.             LPExit(1);
  114.         UI_OpenView(debugView);
  115.     }
  116.     debugLog = open("console.log",O_CREAT+O_RDWR+O_TRUNC);
  117.     if (!gBackgroundOnly) {
  118.         if (debugLog == -1) {
  119.             fprintf(stdout,"Couldn't open \"console.log\".\n");
  120.             fflush(stdout);
  121.         }
  122.     }
  123. }
  124.  
  125. void CloseConsole()
  126. {
  127.     if (debugLog != -1) {
  128.         close(debugLog);
  129.         debugLog = -1;
  130.     }
  131.     if (debugView) {
  132.         UI_DestructView(debugView);
  133.     }
  134. }
  135.  
  136. UI_ViewPtr DoGetWindow(struct interactive *ip)
  137. {
  138.     UI_ViewPtr theView;
  139.  
  140.     theView = UI_LoadView(kConsoleView);
  141.     if (theView) {
  142.         UI_OpenView(theView);
  143.         ((UI_ViewPeek)theView)->viewRef = (long)ip;
  144.     }
  145.     return theView;
  146. }
  147.  
  148. void DoDisposeWindow(UI_ViewPtr view)
  149. {
  150.     UI_DestructView(view);
  151. }
  152.  
  153. static void DoConsoleDrawCaret(TConsoleHdl hConsole)
  154. {
  155.     Rect        util;
  156.     FontInfo    info;
  157.     short        starty;
  158.     short        height;
  159.     Rect        bounds;
  160.     cmdbuf        *cb;
  161.     PenState    penState;
  162.  
  163.     GetPenState(&penState);
  164.     PenMode(patXor);
  165.     
  166.     PenPat(&qd.black);
  167.  
  168.     TextFont(monaco);
  169.     TextSize(9);
  170.     GetFontInfo(&info);
  171.     cb = (*hConsole)->cb;
  172.     bounds = (*hConsole)->bounds;
  173.     starty = info.ascent + info.leading;
  174.     height = info.ascent + info.descent + info.leading;
  175.     util.bottom = height * (cb->first - (*hConsole)->topoffset + 1);
  176.     util.top = util.bottom - height - 1;
  177.     util.left = (*hConsole)->penx;
  178.     util.right = util.left + info.widMax;
  179.     PaintRect(&util);
  180.     SetPenState(&penState);
  181.  
  182.     (*hConsole)->caretVisible = !(*hConsole)->caretVisible;
  183. }
  184.  
  185. static void DoConsoleShowCaret(TConsoleHdl hConsole)
  186. {
  187.     if (!(*hConsole)->consoleActive)
  188.         return;
  189.     if ((*hConsole)->caretHidden) {
  190. //        if (!(*hConsole)->caretVisible)
  191. //            DoConsoleDrawCaret(hConsole);
  192.         (*hConsole)->caretHidden = false;
  193.         (*hConsole)->caretTime = TickCount() + GetCaretTime();
  194.     }
  195. }
  196.  
  197. static void DoConsoleHideCaret(TConsoleHdl hConsole)
  198. {
  199.     if (!(*hConsole)->consoleActive)
  200.         return;
  201.     if (!(*hConsole)->caretHidden) {
  202.         if ((*hConsole)->caretVisible)
  203.             DoConsoleDrawCaret(hConsole);
  204.         (*hConsole)->caretHidden = true;
  205.         (*hConsole)->caretTime = 0;
  206.     }
  207. }
  208.  
  209. static void DoConsoleActivate(TConsoleHdl hConsole)
  210. {
  211.     if (!(*hConsole)->consoleActive) {
  212.         (*hConsole)->consoleActive = true;
  213.         DoConsoleShowCaret(hConsole);
  214.     }
  215. }
  216.  
  217. static void DoConsoleDeactivate(TConsoleHdl hConsole)
  218. {
  219.     if ((*hConsole)->consoleActive) {
  220.         DoConsoleHideCaret(hConsole);
  221.         (*hConsole)->consoleActive = false;
  222.     }
  223. }
  224.  
  225. static void DoConsoleIdle(TConsoleHdl hConsole)
  226. {
  227.     long ticks;
  228.  
  229.     if (!(*hConsole)->consoleActive)
  230.         return;
  231.     if (!(*hConsole)->caretHidden &&
  232.         (ticks = TickCount()) >= (*hConsole)->caretTime)
  233.     {
  234.         DoConsoleDrawCaret(hConsole);
  235.         (*hConsole)->caretTime = ticks + GetCaretTime();
  236.     }
  237. }
  238.  
  239. static short    _starty;
  240. static short    _height;
  241. static short    _linenum;
  242. static Rect        _bounds;
  243. static FontInfo    _finfo;
  244.  
  245. static void DoConsoleDrawLine(char *text)
  246. {
  247.     short    i;
  248.     short    x;
  249.     short    len;
  250.     short    currx;
  251.     short    start;
  252.  
  253.     i = 0;
  254.     len = strlen(text);
  255.     
  256.     start = _starty + _height * _linenum;
  257.     if (start > _bounds.bottom)
  258.         return;
  259.     MoveTo(_bounds.left, start);
  260.     while (i < len) {
  261.         start = i;
  262.         while (i < len && text[i] != 9)
  263.             i++;
  264.         if (i < len && text[i] == 9) {
  265.             if (i > start) {
  266.                 DrawText(text + start, 0, i-start-1);
  267.             }
  268.             currx = i-1;
  269.             for (x = 0; x <= currx % 8; x++, currx++)
  270.                 DrawChar(' ');
  271.             i += 1;
  272.         } else {
  273.             DrawText(text + start, 0, i-start);
  274.         }
  275.     }
  276.     _linenum += 1;
  277. }
  278.  
  279. static void DoConsoleUpdate(TConsoleHdl hConsole, int topoffset, Boolean doHide)
  280. {
  281.     cmdbuf        *cb;
  282.  
  283.     cb = (*hConsole)->cb;
  284.     if (!cb->first)
  285.         return;
  286.     TextFont(monaco);
  287.     TextSize(9);
  288.     GetFontInfo(&_finfo);
  289.     _bounds = (*hConsole)->bounds;
  290.     _linenum = 0;
  291.     _starty = _bounds.top + _finfo.ascent + _finfo.leading;
  292.     _height = _finfo.ascent + _finfo.descent + _finfo.leading;
  293.     if (doHide)
  294.         DoConsoleHideCaret(hConsole);
  295.     topoffset = (topoffset == -1) ? (*hConsole)->topoffset : topoffset;
  296.     eb_range(cb->edbuf, 1 + topoffset, cb->first, DoConsoleDrawLine, FALSE);
  297.     if ((*hConsole)->outcount) {
  298.         (*hConsole)->outbuffer[(*hConsole)->outcount] = '\0';
  299.         DoConsoleDrawLine((*hConsole)->outbuffer);
  300.     }
  301.     if (doHide)
  302.         DoConsoleShowCaret(hConsole);
  303. }
  304.  
  305. void DoConsoleScroll(UI_ViewPtr view, short h, short v)
  306. {
  307.     Rect r;
  308.     short height;
  309.     FontInfo theFont;
  310.     TConsoleHdl hConsole;
  311.  
  312.     r.left = 0;
  313.     r.top = 0;
  314.     r.right = view->portRect.right - 15;
  315.     r.bottom = view->portRect.bottom - 15;
  316.     if (updateRgn == NULL)
  317.         updateRgn = NewRgn();
  318.     GetFontInfo(&theFont);
  319.     height = theFont.ascent + theFont.descent + theFont.leading;
  320.     ScrollRect(&r, -h, -v * height, updateRgn);
  321.     hConsole = (TConsoleHdl)((UI_ViewPeek)view)->viewAux;
  322.     DoConsoleUpdate(hConsole, UI_GetVScrollValue(view), false);
  323. }
  324.  
  325. void TrackScrollBar(UI_ViewPtr view, short code)
  326. {
  327.     short delta;
  328.     short height;
  329.     short pageSize;
  330.     short currValue;
  331.     FontInfo theFont;
  332.  
  333.     GetFontInfo(&theFont);
  334.     height = theFont.ascent + theFont.descent + theFont.leading;
  335.     pageSize = (view->portRect.bottom - view->portRect.top - 15) / height;
  336.     currValue = UI_GetVScrollValue(view);
  337.     if (code == inUpButton) {
  338.         delta = -1;
  339.     } else if (code == inDownButton) {
  340.         delta = 1;
  341.     } else if (code == inPageUp) {
  342.         delta = -pageSize / 2;
  343.     } else if (code == inPageDown) {
  344.         delta = pageSize / 2;
  345.     } else return;
  346.  
  347.     if (delta > 0) {
  348.         if (currValue == UI_GetVScrollMax(view))
  349.             return;
  350.         if (currValue + delta > UI_GetVScrollMax(view)) {
  351.             delta = UI_GetVScrollMax(view) - currValue;
  352.         }
  353.     } else if (delta < 0) {
  354.         if (currValue == UI_GetVScrollMin(view))
  355.             return;
  356.         if (currValue + delta < UI_GetVScrollMin(view)) {
  357.             delta = -currValue;
  358.         }
  359.     }
  360.     UI_SetVScrollValue(view, currValue + delta);
  361.  
  362.     DoConsoleScroll(view, 0, delta);
  363. }
  364.  
  365. static void DoConsoleKey(TConsoleHdl hConsole, char c)
  366. {
  367.     int            i;
  368.     Point        pen;
  369.     FontInfo    info;
  370.     Rect        util;
  371.     short        penx;
  372.     short        currx;
  373.     short        starty;
  374.     short        height;
  375.     Rect        bounds;
  376.     short        value;
  377.     cmdbuf        *cb;
  378.  
  379.     value = UI_GetVScrollValue((*hConsole)->view);
  380.     if ((*hConsole)->topoffset != value)
  381.     {
  382.         UI_SetVScrollValue((*hConsole)->view, (*hConsole)->topoffset);
  383.         DoConsoleScroll((*hConsole)->view, 0, (*hConsole)->topoffset - value);
  384.     }
  385.  
  386.     TextFont(monaco);
  387.     TextSize(9);
  388.     GetFontInfo(&info);
  389.     penx = (*hConsole)->penx;
  390.     currx = (*hConsole)->currx;
  391.     bounds = (*hConsole)->bounds;
  392.     starty = info.ascent + info.leading;
  393.     height = info.ascent + info.descent + info.leading;
  394.  
  395.     cb = (*hConsole)->cb;
  396.     DoConsoleHideCaret(hConsole);
  397.     MoveTo(penx, starty + height * (cb->first - (*hConsole)->topoffset));
  398.     switch(c)
  399.     {
  400.         case 0:
  401.             return;
  402.  
  403.         case '\n':
  404.             if (updateRgn == NULL)
  405.                 updateRgn = NewRgn();
  406.             if ((bounds.bottom - bounds.top) / height <=
  407.                 cb->first - (*hConsole)->topoffset + 1)
  408.             {
  409.                 ScrollRect(&bounds, 0, -height, updateRgn);
  410.                 util.bottom = bounds.bottom;
  411.                 util.top = bounds.bottom - height;
  412.                 util.left = bounds.left;
  413.                 util.right = bounds.right;
  414.                 EraseRect(&util);
  415.                 (*hConsole)->topoffset += 1;
  416.             }
  417.             currx = 0;
  418.             (*hConsole)->outbuffer[(*hConsole)->outcount] = '\0';
  419.             if (!ec_push()) {
  420.                 cb_append(cb);
  421.                 addblock(cb, (*hConsole)->outbuffer);
  422.                 endblock(cb);
  423.                 ec_pop();
  424.                 cb->first += 1;
  425.             }
  426.             (*hConsole)->outcount = 0;
  427.             DoConsoleUpdate(hConsole, -1, true);
  428.             MoveTo(bounds.left, starty + height *
  429.                 (cb->first - (*hConsole)->topoffset));
  430.             lb_inact(cb->edbuf->lb);
  431.             if ((*hConsole)->topoffset !=
  432.                 UI_GetVScrollMax((*hConsole)->view))
  433.             {
  434.                 UI_NoScrollUpdate = true;
  435.                 UI_SetVScrollMax((*hConsole)->view, (*hConsole)->topoffset);
  436.                 UI_SetVScrollValue((*hConsole)->view, (*hConsole)->topoffset);
  437.             }
  438.             break;
  439.  
  440.         case 7: /* BEL */
  441.             SysBeep(1);
  442.             break;
  443.  
  444.         case 8: /* DEL */
  445.             if (!currx)
  446.                 break;
  447.             Move(-info.widMax, 0);
  448.             util.bottom = starty + height *
  449.                 (cb->first - (*hConsole)->topoffset);
  450.             util.top = util.bottom - height;
  451.             util.left = penx - info.widMax;
  452.             util.right = penx;
  453.             EraseRect(&util);
  454.             currx -= 1;
  455.             if ((*hConsole)->outcount)
  456.                 (*hConsole)->outcount -= 1;
  457.             break;
  458.  
  459.         case 9: /* TAB */
  460.             for (i = 0; i <= currx % 8; i++, currx++)
  461.                 DrawChar(' ');
  462.             (*hConsole)->outbuffer[(*hConsole)->outcount++] = c;
  463.             break;
  464.  
  465.         default:
  466.             if(c > 31) {
  467.                 DrawChar(c);
  468.                 (*hConsole)->outbuffer[(*hConsole)->outcount++] = c;
  469.                 currx++;
  470.             }
  471.     }
  472.     GetPen(&pen);
  473.     (*hConsole)->penx = pen.h;
  474.     (*hConsole)->currx = currx;
  475.     DoConsoleShowCaret(hConsole);
  476. }
  477.  
  478. pascal Boolean XtcDebug(UI_ViewPtr view, UI_Action *action)
  479. {
  480.     switch (action->what)
  481.     {
  482.       case UIA_PreOpenView:
  483.       {
  484.         cmdbuf *cb;
  485.         char *tmpfname;
  486.         TConsoleHdl hConsole;
  487.  
  488.         hConsole = (TConsoleHdl)NewHandleClear(sizeof(TConsole));
  489.         tmpfname = NewPtr(20);
  490.         sprintf(tmpfname, "tmp0%05d", tempCount++);
  491.         cb = cb_new(tmpfname);
  492.         cb->this = 0;
  493.         cb_append(cb);
  494.         (*hConsole)->cb = cb;
  495.         (*hConsole)->view = view;
  496.         (*hConsole)->tmpfname = tmpfname;
  497.         ((UI_ViewPeek)view)->viewAux = (long)hConsole;
  498.         return true;
  499.       }
  500.       case UIA_OpenView:
  501.       {
  502.         Rect bounds;
  503.         GrafPtr savePort;
  504.         TConsoleHdl hConsole;
  505.  
  506.         hConsole = (TConsoleHdl)((UI_ViewPeek)view)->viewAux;
  507.         GetPort(&savePort);
  508.         SetPort(view);
  509.         TextFont(monaco);
  510.         TextSize(9);
  511.         bounds = view->portRect;
  512.         bounds.left += kLEFTSPACE;
  513.         bounds.right -= 15;
  514.         bounds.bottom -= 15;
  515.         (*hConsole)->bounds = bounds;
  516.         (*hConsole)->penx = bounds.left;
  517.         SetPort(savePort);
  518.         return true;
  519.       }
  520.       case UIA_DestructView:
  521.       {
  522.         TConsoleHdl hConsole;
  523.  
  524.         hConsole = (TConsoleHdl)((UI_ViewPeek)view)->viewAux;
  525.         cb_del((*hConsole)->cb);
  526.         DisposPtr((*hConsole)->tmpfname);
  527.         DisposHandle((Handle)hConsole);
  528.         return true;
  529.       }
  530.       case UIA_MouseDown:
  531.       {
  532.         Point thePt;
  533.         GrafPtr savePort;
  534.         TConsoleHdl hConsole;
  535.  
  536.         hConsole = (TConsoleHdl)((UI_ViewPeek)view)->viewAux;
  537.         GetPort(&savePort);
  538.         SetPort(view);
  539.         thePt = action->event.where;
  540.         GlobalToLocal(&thePt);
  541. //        DoConsoleClick(hED, thePt);
  542.         SetPort(savePort);
  543.         return true;
  544.       }
  545.       case UIA_PostUpdate:
  546.       {
  547.         GrafPtr savePort;
  548.         TConsoleHdl hConsole;
  549.  
  550.         hConsole = (TConsoleHdl)((UI_ViewPeek)view)->viewAux;
  551.         GetPort(&savePort);
  552.         SetPort(view);
  553.         DoConsoleUpdate(hConsole, -1, true);
  554.         SetPort(savePort);
  555.         return true;
  556.       }
  557.       case UIA_TrackMouseInVScroll:
  558.       {
  559.         Rect r;
  560.         Point pt;
  561.         short value;
  562.         RgnHandle theRgn;
  563.         RgnHandle saveClip;
  564.  
  565.         if (action->code == inThumb) {
  566.             short code;    
  567.             UI_ViewHeaderHdl viewHdl;
  568.  
  569.             pt = action->event.where;
  570.             GlobalToLocal(&pt);
  571.  
  572.             viewHdl = ((UI_ViewPeek)view)->viewHdl;
  573.             value = UI_GetVScrollValue(view);
  574.             code = TrackControl((*viewHdl)->windowVScroll, pt, NULL);
  575.             if (!code)
  576.                 return true;
  577.         }
  578.         r.top = 0;
  579.         r.left = 0;
  580.         r.bottom = view->portRect.bottom - 15;
  581.         r.right = view->portRect.right - 15;
  582.         theRgn = NewRgn();
  583.         saveClip = NewRgn();
  584.         GetClip(saveClip);
  585.         RectRgn(view->clipRgn, &r);
  586.         if (action->code == inThumb) {
  587.             DoConsoleScroll(view, 0, UI_GetVScrollValue(view) - value);
  588.         } else if (action->code == inUpButton) {
  589.             TrackScrollBar(view, action->code);
  590.         } else if (action->code == inDownButton) {
  591.             TrackScrollBar(view, action->code);
  592.         } else if (action->code == inPageUp) {
  593.             TrackScrollBar(view, action->code);
  594.         } else if (action->code == inPageDown) {
  595.             TrackScrollBar(view, action->code);
  596.         }
  597.         SetClip(saveClip);
  598.         DisposeRgn(saveClip);
  599.         DisposeRgn(theRgn);
  600.         return true;
  601.       }
  602.       case UIA_Activate:
  603.       {
  604.         GrafPtr savePort;
  605.         TConsoleHdl hConsole;
  606.  
  607.         hConsole = (TConsoleHdl)((UI_ViewPeek)view)->viewAux;
  608.         GetPort(&savePort);
  609.         SetPort(view);
  610.         DoConsoleActivate(hConsole);
  611.         SetPort(savePort);
  612.         return true;
  613.       }
  614.       case UIA_Deactivate:
  615.       {
  616.         GrafPtr savePort;
  617.         TConsoleHdl hConsole;
  618.  
  619.         hConsole = (TConsoleHdl)((UI_ViewPeek)view)->viewAux;
  620.         GetPort(&savePort);
  621.         SetPort(view);
  622.         DoConsoleDeactivate(hConsole);
  623.         SetPort(savePort);
  624.         return true;
  625.       }
  626.     }
  627.     return XtcMain(view, action);
  628. }
  629.  
  630. pascal Boolean XtcConsole(UI_ViewPtr view, UI_Action *action)
  631. {
  632.     switch (action->what)
  633.     {
  634.       case UIA_PreOpenView:
  635.       {
  636.         cmdbuf *cb;
  637.         char *tmpfname;
  638.         TConsoleHdl hConsole;
  639.  
  640.         hConsole = (TConsoleHdl)NewHandleClear(sizeof(TConsole));
  641.         tmpfname = NewPtr(20);
  642.         sprintf(tmpfname, "tmp0%05d", tempCount++);
  643.         cb = cb_new(tmpfname);
  644.         cb->this = 0;
  645.         cb_append(cb);
  646.         (*hConsole)->cb = cb;
  647.         (*hConsole)->view = view;
  648.         (*hConsole)->tmpfname = tmpfname;
  649.         ((UI_ViewPeek)view)->viewAux = (long)hConsole;
  650.         return true;
  651.       }
  652.       case UIA_OpenView:
  653.       {
  654.         Rect bounds;
  655.         GrafPtr savePort;
  656.         TConsoleHdl hConsole;
  657.  
  658.         hConsole = (TConsoleHdl)((UI_ViewPeek)view)->viewAux;
  659.         GetPort(&savePort);
  660.         SetPort(view);
  661.         bounds = view->portRect;
  662.         bounds.left += kLEFTSPACE;
  663.         bounds.right -= 15;
  664.         bounds.bottom -= 15;
  665.         (*hConsole)->bounds = bounds;
  666.         (*hConsole)->penx = bounds.left;
  667.         SetPort(savePort);
  668.         return true;
  669.       }
  670.       case UIA_DestructView:
  671.       {
  672.         TConsoleHdl hConsole;
  673.  
  674.         hConsole = (TConsoleHdl)((UI_ViewPeek)view)->viewAux;
  675.         cb_del((*hConsole)->cb);
  676.         DisposPtr((*hConsole)->tmpfname);
  677.         DisposHandle((Handle)hConsole);
  678.         return true;
  679.       }
  680.       case UIA_MouseDown:
  681.       {
  682.         Point thePt;
  683.         GrafPtr savePort;
  684.         TConsoleHdl hConsole;
  685.  
  686.         hConsole = (TConsoleHdl)((UI_ViewPeek)view)->viewAux;
  687.         GetPort(&savePort);
  688.         SetPort(view);
  689.         thePt = action->event.where;
  690.         GlobalToLocal(&thePt);
  691. //        DoConsoleClick(hConsole, thePt);
  692.         SetPort(savePort);
  693.         return true;
  694.       }
  695.       case UIA_KeyDown:
  696.       case UIA_AutoKey:
  697.       {
  698.         GrafPtr savePort;
  699.         TConsoleHdl hConsole;
  700.         struct interactive *ip;
  701.         char key = action->event.message & charCodeMask;
  702.  
  703.         if (action->event.modifiers & cmdKey)
  704.             return true;
  705.         ip = (struct interactive *)((UI_ViewPeek)view)->viewRef;
  706.         hConsole = (TConsoleHdl)((UI_ViewPeek)view)->viewAux;
  707.         if ((*hConsole)->incount >= KBUFFSIZE)
  708.             return true;
  709.         GetPort(&savePort);
  710.         SetPort(view);
  711.         if (!ip->noecho) {
  712.             DoConsoleKey(hConsole, key);
  713.         }
  714.         (*hConsole)->inbuffer[(*hConsole)->incount++] = key;
  715.         SetPort(savePort);
  716.         return true;
  717.       }
  718.       case UIA_PostUpdate:
  719.       {
  720.         GrafPtr savePort;
  721.         TConsoleHdl hConsole;
  722.  
  723.         hConsole = (TConsoleHdl)((UI_ViewPeek)view)->viewAux;
  724.         GetPort(&savePort);
  725.         SetPort(view);
  726.         DoConsoleUpdate(hConsole, -1, true);
  727.         SetPort(savePort);
  728.         return true;
  729.       }
  730.       case UIA_TrackMouseInVScroll:
  731.       {
  732.         Rect r;
  733.         Point pt;
  734.         short value;
  735.         RgnHandle theRgn;
  736.         RgnHandle saveClip;
  737.  
  738.         if (action->code == inThumb) {
  739.             short code;    
  740.             UI_ViewHeaderHdl viewHdl;
  741.  
  742.             pt = action->event.where;
  743.             GlobalToLocal(&pt);
  744.  
  745.             viewHdl = ((UI_ViewPeek)view)->viewHdl;
  746.             value = UI_GetVScrollValue(view);
  747.             code = TrackControl((*viewHdl)->windowVScroll, pt, NULL);
  748.             if (!code)
  749.                 return true;
  750.         }
  751.         r.top = 0;
  752.         r.left = 0;
  753.         r.bottom = view->portRect.bottom - 15;
  754.         r.right = view->portRect.right - 15;
  755.         theRgn = NewRgn();
  756.         saveClip = NewRgn();
  757.         GetClip(saveClip);
  758.         RectRgn(view->clipRgn, &r);
  759.         if (action->code == inThumb) {
  760.             DoConsoleScroll(view, 0, UI_GetVScrollValue(view) - value);
  761.         } else if (action->code == inUpButton) {
  762.             TrackScrollBar(view, action->code);
  763.         } else if (action->code == inDownButton) {
  764.             TrackScrollBar(view, action->code);
  765.         } else if (action->code == inPageUp) {
  766.             TrackScrollBar(view, action->code);
  767.         } else if (action->code == inPageDown) {
  768.             TrackScrollBar(view, action->code);
  769.         }
  770.         SetClip(saveClip);
  771.         DisposeRgn(saveClip);
  772.         DisposeRgn(theRgn);
  773.         return true;
  774.       }
  775.       case UIA_Activate:
  776.       {
  777.         GrafPtr savePort;
  778.         TConsoleHdl hConsole;
  779.  
  780.         hConsole = (TConsoleHdl)((UI_ViewPeek)view)->viewAux;
  781.         GetPort(&savePort);
  782.         SetPort(view);
  783.         DoConsoleActivate(hConsole);
  784.         SetPort(savePort);
  785.         return true;
  786.       }
  787.       case UIA_Deactivate:
  788.       {
  789.         GrafPtr savePort;
  790.         TConsoleHdl hConsole;
  791.  
  792.         hConsole = (TConsoleHdl)((UI_ViewPeek)view)->viewAux;
  793.         GetPort(&savePort);
  794.         SetPort(view);
  795.         DoConsoleDeactivate(hConsole);
  796.         SetPort(savePort);
  797.         return true;
  798.       }
  799.       case UIA_Timer:
  800.       {
  801.         GrafPtr savePort;
  802.         TConsoleHdl hConsole;
  803.  
  804.         hConsole = (TConsoleHdl)((UI_ViewPeek)view)->viewAux;
  805.         GetPort(&savePort);
  806.         SetPort(view);
  807.         DoConsoleIdle(hConsole);
  808.         SetPort(savePort);
  809.         return true;
  810.       }
  811.     }
  812.     return XtcMain(view, action);
  813. }
  814.  
  815. int DoWindowRead(struct interactive *ip)
  816. {
  817.     UI_ViewPtr view;
  818.     TConsoleHdl hConsole;
  819.  
  820.     if (!ip)
  821.         return -1;
  822.     view = (UI_ViewPtr)(ip->window);
  823.     hConsole = (TConsoleHdl)((UI_ViewPeek)view)->viewAux;
  824.     if ((*hConsole)->incount) {
  825.         return (*hConsole)->inbuffer[--(*hConsole)->incount];
  826.     }
  827.     return -1;
  828. }
  829.  
  830. int DoWindowWrite(struct interactive *ip,
  831.         const char *buf, unsigned long nbyte)
  832. {
  833.     int i;
  834.     int tab;
  835.     FontInfo info;
  836.     Boolean flushed;
  837.     UI_ViewPtr view;
  838.     GrafPtr savePort;
  839.     TConsoleHdl hConsole;
  840.  
  841.     if (!ip)
  842.         return -1;
  843.     view = (UI_ViewPtr)(ip->window);
  844.     GetPort(&savePort);
  845.     SetPort(view);
  846.     GetFontInfo(&info);
  847.     flushed = (nbyte == 0);
  848.     hConsole = (TConsoleHdl)((UI_ViewPeek)view)->viewAux;
  849.     for(i = 0; i < nbyte; i++) {
  850.         if (*buf == '\n') {
  851.             flushed = true;
  852.             DoConsoleKey(hConsole, *buf);
  853.         } else if ((*hConsole)->outcount < KBUFFSIZE) {
  854.             flushed = false;
  855.             if (*buf == 8 && (*hConsole)->currx) {
  856.                 (*hConsole)->currx -= 1;
  857.                 (*hConsole)->penx -= info.widMax;
  858.                 if ((*hConsole)->outcount)
  859.                     (*hConsole)->outcount -= 1;
  860.             } else if (*buf == 9) {
  861.                 for (tab = 0; tab <= (*hConsole)->currx % 8; tab++, (*hConsole)->currx++)
  862.                     (*hConsole)->penx += info.widMax;
  863.                 (*hConsole)->outbuffer[(*hConsole)->outcount++] = *buf;
  864.             } else if (*buf > 31) {
  865.                 (*hConsole)->outbuffer[(*hConsole)->outcount++] = *buf;
  866.                 (*hConsole)->currx += 1;
  867.                 (*hConsole)->penx += info.widMax;
  868.             }
  869.         }
  870.         buf += 1;
  871.     }
  872.     if (!flushed) {
  873.         DoConsoleUpdate(hConsole, -1, true);
  874.     }
  875.     SetPort(savePort);
  876.     return nbyte;
  877. }
  878.  
  879. int write(int fildes, const char* buf, unsigned long nbyte)
  880. {
  881.     int    i;
  882.     int tab;
  883.     FontInfo info;
  884.     Boolean flushed;
  885.     GrafPtr savePort;
  886.     TConsoleHdl hConsole;
  887.  
  888.     if (fildes < 0 || fildes >2)
  889.         return __write(fildes, buf, nbyte);
  890.  
  891.     if (debugLog != -1)
  892.         __write(debugLog, buf, nbyte);
  893.     if (!debugView)
  894.         return nbyte;
  895.     GetPort(&savePort);
  896.     SetPort(debugView);
  897.     GetFontInfo(&info);
  898.     flushed = (nbyte == 0);
  899.     hConsole = (TConsoleHdl)((UI_ViewPeek)debugView)->viewAux;
  900.     for(i = 0; i < nbyte; i++) {
  901.         if (*buf == '\n') {
  902.             flushed = true;
  903.             DoConsoleKey(hConsole, *buf);
  904.         } else if ((*hConsole)->outcount < KBUFFSIZE) {
  905.             flushed = false;
  906.             if (*buf == 8 && (*hConsole)->currx) {
  907.                 (*hConsole)->currx -= 1;
  908.                 (*hConsole)->penx -= info.widMax;
  909.                 if ((*hConsole)->outcount)
  910.                     (*hConsole)->outcount -= 1;
  911.             } else if (*buf == 9) {
  912.                 for (tab = 0; tab <= (*hConsole)->currx % 8; tab++, (*hConsole)->currx++)
  913.                     (*hConsole)->penx += info.widMax;
  914.                 (*hConsole)->outbuffer[(*hConsole)->outcount++] = *buf;
  915.             } else if (*buf > 31) {
  916.                 (*hConsole)->outbuffer[(*hConsole)->outcount++] = *buf;
  917.                 (*hConsole)->currx += 1;
  918.                 (*hConsole)->penx += info.widMax;
  919.             }
  920.         }
  921.         buf += 1;
  922.     }
  923.     if (!flushed) {
  924.         DoConsoleUpdate(hConsole, -1, true);
  925.     }
  926.     SetPort(savePort);
  927.     return nbyte;
  928. }
  929.